package Job202213;

import java.security.MessageDigest;
import java.util.Random;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.01.16.16.54
 */
public class Util {
    //随机生成指定长度的密码或指定长度的验证码字符串。
    public static void main(String[] args) {
        //getPassword(4);
        System.out.println(getPassword());
    }
    public static String getPassword(int... n) {
        int num = n.length == 0 ? 6 : n[0];
        String str = "0123456789_-=+'\",`~!@#$%^&*()abcdefghijklmnolpqrstuvwxyzABCDEFGHIJKLMNOLPQRSTUVWXYZ";
        StringBuilder sbu = new StringBuilder(6);
        Random rand = new Random();
        for (int i = 0; i < num; i++) {
            sbu.append(str.charAt(rand.nextInt(str.length())));
        }
        return sbu.toString();
    }

    public String checkcode() {
        return checkcode(4);
    }

    public String checkcode(int n) {
        String str = "0123456789abcdefghijklmnolpqrstuvwxyzABCDEFGHIJKLMNOLPQRSTUVWXYZ";
        StringBuilder sbu = new StringBuilder(6);
        Random rand = new Random();
        for (int i = 0; i < n; i++) {
            sbu.append(str.charAt(rand.nextInt(str.length())));
        }
        return sbu.toString();
    }
    //md5(32) sha1(40)
    public String getMdString(String password, String algorithm) {
        StringBuilder sbu = new StringBuilder();
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance(algorithm);
        } catch (Exception e) {
            e.printStackTrace();
        }
        md.update(password.getBytes());
        for (byte b : md.digest()) {
            sbu.append(String.format("%02x", b));
        }
        return sbu.toString();
    }
}
